package com.twu.biblioteca;

import java.io.*;
import java.util.*;
/**
 * Created with IntelliJ IDEA.
 * User: kush jain
 * Date: 7/4/13
 * Time: 3:34 PM
 * To change this template use File | Settings | File Templates.
 */
public class transactionManagaer implements Serializable {
    ArrayList reservationTxns;
    int nreservationTxns;
    public  void  addReservationTransaction(int bookid)
    {
        reservationTxns.add(new bookTransaction(nreservationTxns+1,bookid));
        nreservationTxns++;
    }
    public void freeBook(int bookID,inventoryManager libInv)
    {
        if(this.getReservedBooks(libInv).getBookByBookID(bookID)==null)  {
            Presenter.Say("This book is not reserved");
            return;
        }
        for(int i=0;i<nreservationTxns;i++)
        {
            if(((bookTransaction)reservationTxns.get(i)).getReservationStatus()==true && ((bookTransaction)reservationTxns.get(i)).getBookID()==bookID)
            {
                    ((bookTransaction)reservationTxns.get(i)).setFree();
                    Presenter.Say("This book has been freed");
                    return;
            }
        }
    }
    transactionManagaer()
    {
        reservationTxns = new ArrayList();
    }
    public boolean isReserved(int bookid)
    {
        for(int i=0;i<nreservationTxns;i++)
        {
            if(((bookTransaction)reservationTxns.get(i)).getBookID()==bookid)
            {
                return ((bookTransaction)reservationTxns.get(i)).getReservationStatus();
            }
        }
        return false;
    }
    public inventoryManager getReservedBooks(inventoryManager libInventory)
    {
        inventoryManager reservedBooks = new inventoryManager();
        for(int i=0;i<nreservationTxns;i++)
        {
            if(((bookTransaction)reservationTxns.get(i)).getReservationStatus()==true)
            {
                  reservedBooks.addNewBook(libInventory.getBookByBookID(((bookTransaction)reservationTxns.get(i)).getBookID()));
            }
        }
        return reservedBooks;
    }


}
